<template>
  <section>
    <el-col v-show="pwdVisible">
      <el-form label-width="6rem" ref="newForm" class="pwdForm">
        <el-form-item label="请输入密码">
          <el-input
            v-model="initializePwd"
            type="password"
            placeholder="请输入初始化密码"
            style="width:15rem"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item>
        
        </el-form-item>-->
      </el-form>
      <h2 class="pwdTitle">注：初始化房间信息前，请先备份数据库，以免想恢复到以前状态时数据丢失</h2>
      <el-button type="primary" class="pwdButton" @click.native="confirmPwd">确认</el-button>
    </el-col>
    <el-form
      label-width="8rem"
      ref="initializeForm"
      class="initializeForm"
      v-show="formVisible"
      :model="initializeData"
      :loading="initializeLoading"
      :rules="rules"
      v-loading="loading"
    >
      <el-form-item label="所属区域" prop="_area">
        <el-select v-model="initializeData._area" placeholder="区域编号" @change="areaChange">
          <el-option v-for="item in area" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="宿舍楼编号" prop="_dorm">
        <el-select
          v-model="initializeData._dorm"
          placeholder="宿舍楼编号"
          @change="dormChange(initializeData._dorm)"
        >
          <el-option v-for="item in dorm" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="初始化楼层" required>
        <el-col :span="11">
          <el-form-item prop="beginLayers">
            <el-select v-model="initializeData.beginLayers" placeholder="楼层">
              <el-option
                v-for="item in layers"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col class="line" :span="1" :offset="1">-</el-col>
        <el-col :span="11">
          <el-form-item prop="endLayers">
            <el-select v-model="initializeData.endLayers" placeholder="楼层">
              <el-option
                v-for="item in layers"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="房间数" prop="roomCount">
        <el-input v-model.number="initializeData.roomCount"></el-input>
      </el-form-item>
      <el-form-item label="房间类型" prop="roomType">
        <el-radio-group v-model="initializeData.roomType">
          <el-radio label="0">单人间</el-radio>
          <el-radio label="1">多人间</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="入住性别" prop="roomSex">
        <el-radio-group v-model="initializeData.roomSex">
          <el-radio label="1">男</el-radio>
          <el-radio label="2">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="房间编号前缀" prop="roomAnti">
        <el-input v-model="initializeData.roomAnti"></el-input>
      </el-form-item>
      <el-form-item label="可住人数" prop="_number">
        <el-select v-model="initializeData._number" placeholder="人数">
          <el-option
            v-for="item in personNumber"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="柜子数量">
        <el-select v-model="initializeData.cupboard" placeholder="柜子数量">
          <el-option
            v-for="item in cupboard"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="住宿费用" prop="roomCost">
        <el-input v-model.number="initializeData.roomCost"></el-input>
      </el-form-item>
      <el-form-item label="费用承担" prop="costBear">
        <el-radio-group v-model="initializeData.costBear">
          <el-radio label="1">公司承担</el-radio>
          <el-radio label="2">员工承担</el-radio>
          <el-radio label="3">公摊</el-radio>
          <el-radio label="4">现结类冷水(洗衣机类型)</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="默认房间状态" prop="roomStatus">
        <el-radio-group v-model="initializeData.roomStatus">
          <el-radio label="1">启用</el-radio>
          <el-radio label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否临时入住" prop="iSTemporary">
        <el-radio-group v-model="initializeData.iSTemporary">
          <el-radio label="1">是</el-radio>
          <el-radio label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button @click.native="resettingSubmit">重置</el-button>
        <el-button type="primary" @click.native="initializeSubmit('initializeForm')">确认</el-button>
      </el-form-item>
    </el-form>
  </section>
</template>
<script>
import { getArea } from "../../api/area.js";
import { getDorm, getFloor } from "../../api/dorm.js";
import { roomInitializeConfirmPwd, roomInitialize } from "../../api/room.js";
export default {
  data() {
    return {
      initializePwd: null,
      pwdVisible: true,
      formVisible: false,
      initializeLoading: false,
      initializeData: {
        _area: null,
        _dorm: null,
        beginLayers: null,
        endLayers: null,
        roomCount: null,
        roomType: null,
        roomSex: null,
        roomAnti: null,
        _number: null,
        roomCost: null,
        roomStatus: null,
        iSTemporary: null,
        cupboard: 0,
        costBear: null
      },
      area: [
        {
          value: "",
          label: ""
        }
      ],
      dorm: [
        {
          value: "",
          label: ""
        }
      ],
      layers: [],
      personNumber: [
        {
          value: "1",
          label: "1"
        },
        {
          value: "2",
          label: "2"
        },
        {
          value: "3",
          label: "4"
        },
        {
          value: "4",
          label: "4"
        },
        {
          value: "5",
          label: "5"
        },
        {
          value: "6",
          label: "6"
        }
      ],
      cupboard: [
        {
          value: "0",
          label: "0"
        },
        {
          value: "1",
          label: "1"
        },
        {
          value: "2",
          label: "2"
        },
        {
          value: "3",
          label: "4"
        },
        {
          value: "4",
          label: "4"
        },
        {
          value: "5",
          label: "5"
        },
        {
          value: "6",
          label: "6"
        },
        {
          value: "7",
          label: "7"
        },
        {
          value: "8",
          label: "8"
        }
      ],
      rules: {
        _area: [{ required: true, message: "请选择区域", trigger: "change" }],
        _dorm: [{ required: true, message: "请选择宿舍楼", trigger: "change" }],
        beginLayers: [
          { required: true, message: "请选择起始楼层", trigger: "change" }
        ],
        endLayers: [
          { required: true, message: "请选择结束楼层", trigger: "change" }
        ],
        roomCount: [
          { required: true, message: "请输入房间数量", trigger: "blur" }
        ],
        roomType: [
          { required: true, message: "请选择房间类型", trigger: "change" }
        ],
        roomSex: [
          { required: true, message: "请选择入住性别", trigger: "change" }
        ],
        _number: [
          { required: true, message: "请选择入住人数", trigger: "change" }
        ],
        roomCost: [
          { required: true, message: "请输入房间费用", trigger: "blur" }
        ],
        roomStatus: [
          { required: true, message: "请选择结束楼层", trigger: "change" }
        ],
        iSTemporary: [
          { required: true, message: "请选择结束楼层", trigger: "change" }
        ],
        costBear: [
          { required: true, message: "请选择费用承担方", trigger: "change" }
        ]
      },
      loading: false
    };
  },
  methods: {
    getArea() {
      getArea().then(res => {
        if (res) {
          this.area = res.msg;
        }
      });
    },
    areaChange() {
      if (this.initializeData._area === "") {
        return true;
      }
      getDorm(this.initializeData._area).then(res => {
        if (res) {
          this.initializeData._dorm = null;
          this.dorm = res.msg;
        } else {
          this.dorm = [
            {
              value: "",
              label: ""
            }
          ];
          this.initializeData._dorm = "";
        }
      });
    },
    // 密码确认
    confirmPwd() {
      // TODO:
      roomInitializeConfirmPwd(this.initializePwd).then(res => {
        if (res) {
          this.pwdVisible = false;
          this.formVisible = true;
        }
      });
    },
    initializeSubmit(form) {
      // TODO: 初始化
      this.$refs[form].validate(valid => {
        if (valid) {
          this.loading = true;
          roomInitialize(this.initializeData).then(res => {
            this.loading = false;
            if (res) {
              this.$alert(res.msg, "操作提示", {
                confirmButtonTextL: "确定",
                callback: action => {
                  this.alterFormVisible = false;
                }
              });
            }
          });
        } else {
          this.$message.error("信息未填完整");
        }
      });
    },
    // 重置
    resettingSubmit() {
      this.initializeData = {
        _area: null,
        _dorm: null,
        beginLayers: null,
        endLayers: null,
        roomCount: null,
        roomType: null,
        roomSex: null,
        roomAnti: null,
        number: null,
        roomCost: null,
        roomStatus: null,
        iSTemporary: null
      };
    },
    dormChange(dorm) {
      if (dorm === "") {
        return true;
      }
      // 获取当前楼层
      getFloor(this.initializeData._area, this.initializeData._dorm).then(
        res => {
          if (res) {
            this.layers = [];
            let floor = res.msg[0].d_layers;
            for (let i = 1; i <= floor; i++) {
              let _floor = { value: i, label: i };
              this.layers.push(_floor);
            }
          }
        }
      );
    }
  },
  mounted() {
    this.getArea();
  }
};
</script>
<style>
.pwdForm {
  /* 实现元素居中 */
  position: absolute;
  top: 20%; /* 上浮一点 */
  left: 50%;
  transform: translate(-50%, -50%);
  /* align-self: center  此属性目前暂未支持，书上看来的，一键垂直居中*/
}
.pwdTitle {
  position: absolute;
  top: 30%; /* 上浮一点 */
  left: 50%;
  transform: translate(-50%, -50%);
  color: rgb(243, 51, 51);
  font-size: 1.5rem;
}
.pwdButton {
  position: absolute;
  top: 50%; /* 上浮一点 */
  left: 50%;
  transform: translate(-50%, -50%);
  width: 10rem;
}
.initializeForm {
  width: 45em;
  /* height: 55em; */
  padding: 1.5em 1.5em;
  border: 1px solid rgb(178, 194, 184);
  border-radius: 2%;
  /* position: absolute; */
  /* top: 50%; 上浮一点 */
  /* left: 50%; */
  /* transform: translate(-50%, -50%); */
  margin: auto;
}
</style>